/**
 *
 * @author Axel Huizinga
 * @author  inspired from Tony Polinelli
 */

package me.cunity.tools;

import haxe.Timer;

class GlobalTimer
{
    public static var timers:Array<Timer> = new Array();
	
	static var _init:Float = Date.now().getTime();
	
	
	public static function getTimer():Int {
		//trace( Date.now().getTime() +' - ' + _init);
		return Std.int(Date.now().getTime() - _init);
	}
	
	public static function setInterval(func:Dynamic, milliseconds:Int,
		args:Array<Dynamic>=null):Int
    {
        var timer:Timer = new Timer(milliseconds);
        timers.push(timer);
        var id = timers.length - 1;
        timer.run = function()
        {
           Reflect.callMethod(null, func, args==null?[]:args);
        }

        return id;
    }

    public static function clearInterval(id:Int)
    {
        timers[id].stop();
        timers[id] = null;
    }

    public static function setTimeout(func:Dynamic, milliseconds:Int,
		args:Array<Dynamic>=null):Int
    {
        var timer:Timer = new Timer(milliseconds);
        timers.push(timer);
        var id = timers.length-1;
        timer.run = function() {
            clearTimeout(id);
			Reflect.callMethod(null, func, args == null?[]:args);
        }

        return id;
    }

    public static function clearTimeout(id:Int)
    {
        timers[id].stop();
        timers[id] = null;
		//trace(id + ':' + timers[id]);
    }

}


class GetTimer 
{
	
	
	
}